package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 广告位请求处理类
 */
@RestController
@RequestMapping("ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;



    /**
     *分页条件查询 广告位,和对应图片数量
     * @param page 页码
     * @param limit 页面条目数
     * @param yddAdvpos 查询条件,广告名,广告描述
     * @return 分页查询对象
     */
    @GetMapping("list")
    public ResultData getList(Long page, Long limit, YddAdvpos yddAdvpos){
        log.info("广告位分页查询条件:page={},limit={},yddAdvpos={}",page,limit,yddAdvpos);
        //获取查询结果
        PageResult pageResult = yddAdvposService.getList(page, limit, yddAdvpos);
        log.info("查询结果为:{}",pageResult);
        return ResultData.ok("",pageResult);
    }


    /**
     * 更新广告位信息
     * @param yddAdvpos 需要更新的数据
     * @return  提示信息
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos ){
        log.info("需要更新的数据为:{}",yddAdvpos);
        if (yddAdvpos.getId()==null) return ResultData.error("数据不存在");
        yddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("","修改成功");
    }

    /**
     * 添加广告位信息
     * @param yddAdvpos 添加数据
     * @return  提示信息
     */
    @PostMapping("add")
    public ResultData add(@RequestBody YddAdvpos yddAdvpos){
        log.info("要添加的广告位为:{}",yddAdvpos);
        yddAdvposService.save(yddAdvpos);
        return ResultData.ok("","添加成功");
    }


    /**
     * 根据id获取对应的广告位详细
     * @param id    广告位id
     * @return      对应的广告位数据
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Long id){
        log.info("要修改的广告位id为:{}",id);
        if(id==null) return ResultData.error("该数据不存在");
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        log.info("查询到的数据为:{}",yddAdvpos);
        return ResultData.ok("",yddAdvpos);
    }

    /**
     * 查询所有广告位信息
     * @return 封装所有广告位的集合
     */
    @GetMapping("all")
    public ResultData getAll(){
        log.info("查询所有广告位信息");
        List<YddAdvpos> yddAdvpos = yddAdvposService.list();
        if(yddAdvpos==null || yddAdvpos.size()==0) return ResultData.error("暂无数据");
        log.info("查询到的数据为:{}",yddAdvpos);
        return ResultData.ok("",yddAdvpos);
    }

    /**
     * 根据id删除广告位
     * @param id    广告位id
     * @return  提示信息
     */
    @GetMapping("del")
    public ResultData delete(Long id){
        log.info("要删除的id为:{}",id);
        if(id==null) return ResultData.error("该数据不存在");
        return yddAdvposService.deleteById(id);
    }



}
